package com.kj.tms.system.controller.menu;

import com.kj.tms.common.bean.dto.Pager;
import com.kj.tms.common.bean.dto.ReturnData;
import com.kj.tms.common.utils.constant.Consts;
import com.kj.tms.pri.system.menu.dto.DictDto;
import com.kj.tms.pri.system.menu.service.DictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * com.hongtu.tms.system.controller.module
 * 字典信息表
 *
 * @auther yangruidong
 * @create 2017/10/19
 */
@Controller
@RequestMapping("/system/dict")
public class DictController {

    private static final Logger logger = LoggerFactory.getLogger(DictController.class);
    @Autowired
    private DictService dictServiceImpl;


    @RequestMapping("/index")
    public String index() {
        return "system/dict/index";
    }

    /**
     * @Title: toAdd
     * @Description: 跳转到新增模块
     */

    @RequestMapping("/toAdd")
    public String toAdd() {
        return "system/dict/add";
    }

    /**
     * @Title: toUpdate
     * @Description: 跳转到更新模块
     */

    @RequestMapping("/toUpdate")
    public String toUpdate() {
        return "system/dict/update";
    }


    /**
     * @param @param  model
     * @param @param  response
     * @param @throws Exception
     * @return void 返回类型
     * @throws
     * @Title: queryByPage
     * @Description: 查询分页数据
     */
    @RequestMapping("/queryByPage")
    public
    @ResponseBody
    ReturnData queryByPage(DictDto model) {
        model.setDelMark(Consts.DEL_MARK_0);
        Pager pager = null;
        try {
            pager = dictServiceImpl.queryByPage(model);
        } catch (Exception e) {
            return new ReturnData(Consts.ERROR, "查询分页数据失败", "");
        }
        //设置页面数据
        Map<String, Object> returnObject = new HashMap<String, Object>();
        returnObject.put("total", pager.getRowCount());
        returnObject.put("rows", pager.getResults());
        return new ReturnData(Consts.SUCCESS, "", returnObject);
    }


    /**
     * @param  model
     * @return void 返回类型
     * @throws
     * @Title: ReturnSingleDictDto
     * @Description: 查询返回单个dto
     */
    @RequestMapping("/returnSingleDictDto")
    public
    @ResponseBody
    ReturnData ReturnSingleDictDto(DictDto model) {
        DictDto dto = dictServiceImpl.getByCondition(model);

        if (dto == null) {
            return new ReturnData(Consts.ERROR, "", dto);
        }
        return new ReturnData(Consts.SUCCESS, "", dto);
    }

    /**
     * @param model
     * @return void 返回类型
     * @throws Exception
     * @Title: getTree
     * @Description: 将信息生成树
     */
    @RequestMapping("/getTree")
    public
    @ResponseBody
    ReturnData getTree(DictDto model) {
        return dictServiceImpl.getTree(model);
    }

    /**
     * @Title: save
     * @Description: 添加
     * @param dto
     * @param @throws Exception
     * @return void 返回类型
     * @throws
     */
    @RequestMapping("/save")
    public
    @ResponseBody
    ReturnData save(DictDto dto) {
        return  dictServiceImpl.save(dto);
    }

    /**
     * @param  dto
     * @return void 返回类型
     * @throws
     * @Title: update
     * @Description: 修改数据
     */
    @RequestMapping("/update")
    public
    @ResponseBody
    ReturnData update(DictDto dto) {
        return  dictServiceImpl.updateData(dto);
    }

    /**
     * @param  dto
     * @return void 返回类型
     * @throws
     * @Title: delete
     * @Description: 删除数据以及子节点 修改delMark标志为1
     */
    @RequestMapping("/delete")
    public
    @ResponseBody
    ReturnData delete(DictDto dto) {
        return dictServiceImpl.deleteData(dto);
    }

}
